<?php namespace Common\Model;
use Hdphp\Model\Model;
class Goods extends Model{
	protected $table = 'goods';
	//自动验证
	protected $validate = array(
		array('gname','required','商品名称不能为空',3,3),
		array('gnumber','required','商品货号不能为空',3,3),
//		array('nuit','required','商品单位不能为空',3,3),
		array('marketprice','required','商品市场价格不能为空',3,3),
		array('shopprice','required','商品商城价格不能为空',3,3),
		array('gclick','required','点击次数不能为空',3,3),
		array('gtotal','required','库存不能为空',3,3)
	);
	protected $auto = array(
		array('time','time','function',3,1),
		array('ygshop_admin_aid','getAid','method',3,1),
		//列表图
		array('pic','getPic','method',3,1),
	);
	public function store(){
		//触发自动验证
	    if(!$this->create()) return FALSE;
		$gid = $this->add();
		$goodsAttrModel = new \Common\Model\GoodsAttr;		
			foreach (Q('post.attr') as $k => $v) {
				$data = array(
					'ygshop_type_attr_taid' => $k,
					'gtvalue' => $v,
					'ygshop_goods_gid' => $gid
				);
				$goodsAttrModel->add($data);
			}
			foreach (Q('post.spec') as $k => $v) {
				foreach ($v['color'] as $kk => $vv) {
					$data = array(
						'gtvalue' => $v['color'][$kk],
						'added' => $v['added'][$kk],
						'ygshop_type_attr_taid' => $k,
						'ygshop_goods_gid' => $gid
					);
					$goodsAttrModel->add($data);
				}
			}
			$goodsDetailModel = new \Common\Model\Detail;
			foreach (Q('post.gpic') as $v) {
				$path = $v;
				$type = strrchr($path, '.');
				$smallPath = str_replace($type, '_small' . $type, $path);
				//p($smallPath);exit;
				Image::thumb($path,$smallPath,50,50,6);
				$mediumPath = str_replace($type, '_medium' . $type, $path);
				Image::thumb($path,$mediumPath,200,200,6);
				$bigPath = str_replace($type, '_big' . $type, $path);
				Image::thumb($path,$bigPath,500,500,6);
				$data = array(
					'small' => $smallPath,
					'medium' => $mediumPath,
					'big' => $bigPath,
					'intro' => Q('post.intro'),
					'service' => Q('post.service'),
					'ygshop_goods_gid' => $gid
				);
				$goodsDetailModel->add($data);
			}
		return TRUE;
	}
	public function getAid(){
	    return $_SESSION['info']['aid'];
	}
	//列表图
	public function getPic(){
	    
		if(Q('post.list')){
			foreach (Q('post.list') as $v) {
				return $v;
			}
		}else{
			$this->error = '列表图必须添加';
			return FALSE;
		}
	}
	
}





 ?>